﻿using Donal.Xms.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Donal.Xms.Web.qianyi.dialog
{
    public partial class dialog_select_company : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected string keywords = string.Empty;
        protected int group_id;
        protected int status;
        protected int monthly;
        protected int invoice;
        protected int industry;
        protected string start_time = string.Empty;
        protected string end_time = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.group_id = DTRequest.GetQueryInt("group_id");
            this.status = DTRequest.GetQueryInt("status");
            this.monthly = DTRequest.GetQueryInt("monthly");
            this.invoice = DTRequest.GetQueryInt("invoice");
            this.industry = DTRequest.GetQueryInt("industry");
            this.start_time = DTRequest.GetQueryString("start_time");
            this.end_time = DTRequest.GetQueryString("end_time");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("user_company", DTEnums.ActionEnum.View.ToString()); //检查权限
                                 
                TreeBind("is_lock=0"); //绑定类别
                IndustryBind("is_lock=0");
                RptBind("status=0" + CombSqlTxt(this.keywords), "id desc");
            }
        }

        #region 绑定行业=================================
        private void IndustryBind(string strWhere)
        {
            BLL.user_industry bll = new BLL.user_industry();
            DataTable dt = bll.GetList(0, strWhere, "sort_id,id desc").Tables[0];

            this.ddlIndustry.Items.Clear();
            this.ddlIndustry.Items.Add(new ListItem("所有行业", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlIndustry.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion

        #region 绑定组别=================================
        private void TreeBind(string strWhere)
        {
            BLL.user_groups bll = new BLL.user_groups();
            DataTable dt = bll.GetList(0, strWhere, "id desc").Tables[0];

            this.ddlGroupId.Items.Clear();
            this.ddlGroupId.Items.Add(new ListItem("会员组别", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlGroupId.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            this.txtKeywords.Text = this.keywords;

            if (this.group_id > 0)
            {
                this.ddlGroupId.SelectedValue = this.group_id.ToString();
            }
            if (this.status > 0)
            {
                this.ddlStatus.SelectedValue = this.status.ToString();
            }
            if (this.monthly > 0)
            {
                this.ddlMonthly.SelectedValue = this.monthly.ToString();
            }
            if (this.industry > 0)
            {
                this.ddlIndustry.SelectedValue = this.industry.ToString();
            }
            txtStartTime.Text = start_time;
            txtEndTime.Text = end_time;

            BLL.company bll = new BLL.company();
            this.rptList.DataSource = bll.GetListView(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();

            string pageUrl = Utils.CombUrlTxt("dialog_select_company.aspx", "group_id={0}&status={1}&monthly={2}&invoice={3}&start_time={4}&end_time={5}&keywords={6}&page={7}",
               group_id.ToString(), status.ToString(), monthly.ToString(), invoice.ToString(), start_time.ToString(), end_time.ToString(), this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");

            if (group_id > 0)
            {
                strTemp.Append(" and group_id=" + group_id);
            }
            if (status > 0)
            {
                strTemp.Append(" and status=" + status);
            }
            if (monthly > 0)
            {
                strTemp.Append(" and monthly=" + monthly);
            }
            if (invoice > 0)
            {
                strTemp.Append(" and invoiceType=" + invoice);
            }
            if (industry > 0)
            {
                strTemp.Append(" and industry=" + industry);
            }

            if (!string.IsNullOrEmpty(start_time))
            {
                strTemp.Append(" and datediff(d,add_time,'" + start_time + "')<=0");
            }
            if (!string.IsNullOrEmpty(end_time))
            {
                strTemp.Append(" and datediff(d,add_time,'" + end_time + "')>=0");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (companyName like '%" + _keywords + "%' or industry like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回用户每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("dialog_select_company_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        #region 返回用户状态=============================
        protected string GetUserStatus(int status)
        {
            string result = string.Empty;
            switch (status)
            {
                case 0:
                    result = "正常";
                    break;
                case 1:
                    result = "正常";
                    break;
                case 2:
                    result = "禁用";
                    break;
                case 3:
                    result = "待审核";
                    break;
            }
            return result;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            redirect();
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("dialog_select_company_page_size", "Donal.XmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("dialog_select_company.aspx", "group_id={0}&status={1}&monthly={2}&invoice={3}&start_time={4}&end_time={5}&keywords={6}&page={7}",
                 ddlGroupId.SelectedValue, ddlStatus.SelectedValue, ddlMonthly.SelectedValue, "", txtStartTime.Text, txtEndTime.Text, txtKeywords.Text, "__id__"));
        }
              

        protected void ddlGroupId_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlMonthly_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlInvoiceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlIndustry_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        private void redirect()
        {
            Response.Redirect(Utils.CombUrlTxt("dialog_select_company.aspx", "group_id={0}&status={1}&monthly={2}&invoice={3}&start_time={4}&end_time={5}&keywords={6}&industry={7}",
                          ddlGroupId.SelectedValue, ddlStatus.SelectedValue, ddlMonthly.SelectedValue, "", txtStartTime.Text, txtEndTime.Text, txtKeywords.Text, ddlIndustry.SelectedValue));
        }
    }
}